<template>
  <div class="app-container" v-loading="appLoading">
    <!-- 高级查询 -->

 <el-row :gutter="40" class="mb5">
      <el-col :span="1.5">
        <span style="padding-left: 2px"><el-tag type="info">销售件数合计：{{ wtCntSum }}</el-tag></span>
          <span style="padding-left: 2px"><el-tag type="info">退单件数合计：{{ returnCntSum }}</el-tag></span>
          <span style="padding-left: 2px"><el-tag type="info">卖出金额合计：{{ contractAmountSum }}</el-tag></span>
          <span style="padding-left: 2px"><el-tag type="info">卖出退单价合计：{{ sumSellRtAmt }}</el-tag></span>
          <span style="padding-left: 2px"><el-tag type="info">买入金额合计：{{ amountSum }}</el-tag></span>
           <span style="padding-left: 2px"><el-tag type="info">买入退单合计：{{ returnAmountSum }}</el-tag></span>
        <span style="padding-left: 2px"><el-tag type="info">销售里程：{{ custMilePaymentAmt }}</el-tag></span>
       </el-col>



    </el-row>
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange" @sort-change="handleSortChange">
     <el-table-column label="子订单号" width="185"  align="center" prop="sourceItemId"  />
      <!-- <el-table-column label="商品"  width="205" show-overflow-tooltip align="center" prop="cargoObjectName" />
       <el-table-column label="商城SPU" width="100" align="center"  prop="custSpuId" />
       <el-table-column label="商城SKU"   width="105" align="center" prop="custSkuId" />
       --><el-table-column label="销售数" width="59" align="right" prop="originalSellQty" />
       <el-table-column label="最终数" width="59" align="right" prop="wtCnt" />
       <el-table-column label="买入单价" align="right" width="112"  prop="rate">
         <template slot-scope="scope">
           <span>{{ Number(scope.row.rate).toFixed(2) }}</span>
         </template>
       </el-table-column>
       <el-table-column label="实际买入价" align="right" width="112"  prop="amount">
         <template slot-scope="scope">
           <span>{{ Number(scope.row.amount).toFixed(2) }}</span>
         </template>
       </el-table-column>
     <!--  <el-table-column label="买入合同价" align="right" width="112"  prop="contractRate">
         <template slot-scope="scope">
           <span>{{ Number(scope.row.contractRate).toFixed(2) }}</span>
         </template>
       </el-table-column> -->
       <el-table-column label="卖出单价" align="right" width="112"  prop="amtGenAr">
         <template slot-scope="scope">
           <span>{{ Number(scope.row.amtGenAr).toFixed(2) }}</span>
         </template>
       </el-table-column>
       <el-table-column label="商城单价" align="right" width="112"  prop="custRelSalesUnitPrice">
         <template slot-scope="scope">
           <span>{{ Number(scope.row.custRelSalesUnitPrice).toFixed(2) }}</span>
         </template>
       </el-table-column>
       <!--  <el-table-column label="卖出ji" align="center" sortable width="115" prop="amtCur" />
       --> <el-table-column label="卖出价" align="right" width="72"  prop="contractAmount">
       <template slot-scope="scope">
         <span>{{ Number(scope.row.contractAmount).toFixed(2) }}</span>
       </template>
     </el-table-column>
       <el-table-column label="折后价" align="right" width="72"  prop="salesOriginAmount">
         <template slot-scope="scope">
           <span>{{ Number(scope.row.salesOriginAmount).toFixed(2) }}</span>
         </template>
       </el-table-column>
       <el-table-column label="实际结算价" align="right" width="95"  prop="amtAr">
         <template slot-scope="scope">
           <span>{{ Number(scope.row.amtAr).toFixed(2) }}</span>
         </template>
       </el-table-column>
       <el-table-column label="退单数" align="right" width="59"  prop="returnQty" />

       <el-table-column label="销售退单额" align="right" width="59"  prop="returnAmt">
         <template slot-scope="scope">
           <span>{{ Number(scope.row.returnAmt).toFixed(2) }}</span>
         </template>
       </el-table-column>
       <el-table-column label="店铺退单额" align="right" width="59"  prop="buyReturnAmt">
         <template slot-scope="scope">
           <span>{{ Number(scope.row.buyReturnAmt).toFixed(2) }}</span>
         </template>
       </el-table-column>

       <el-table-column label="退单日期" align="center" prop="returnTime" width="120">
          <template slot-scope="scope">
             <span>{{ parseTime(scope.row.returnTime, '{m}-{d} {h}:{i}:{s}') }}</span>
           </template>
         </el-table-column>
       <el-table-column label="领券现金" align="right" width="72"  prop="storeCuponCash">
         <template slot-scope="scope">
           <span>{{ Number(scope.row.storeCuponCash).toFixed(2) }}</span>
         </template>
       </el-table-column>
       <el-table-column label="退单里程" align="right" width="72" prop="storeCuponMile" />

       <el-table-column label="折扣活动ID" width="95" show-overflow-tooltip align="center" prop="discountActivityCode" />
       <el-table-column label="领券活动ID" width="95" show-overflow-tooltip align="center" prop="storeDiscountActId" />
       <el-table-column label="领券券码" width="95" show-overflow-tooltip align="center" prop="storeDiscountCode" />
       <el-table-column label="里程券" align="center" prop="storeDiscountAmtPoint" />
       <el-table-column label="现金券" align="center" prop="storeDiscountAmt" />
       <el-table-column label="销售现金" align="right" width="75"  prop="custCashPaymentAmt">
         <template slot-scope="scope">
           <span>{{ Number(scope.row.custCashPaymentAmt).toFixed(2) }}</span>
         </template>
       </el-table-column>
       <el-table-column label="销售里程" align="right" prop="custMilePaymentAmt" />
       <el-table-column label="退单号" align="center" prop="returnOrderItemNo" />
       <el-table-column label="店铺" align="center" show-overflow-tooltip width="180">
         <template slot-scope="scope">
           <el-button type="text" @click="handleView(scope.row)">{{ scope.row.storeName }}</el-button>
         </template>
       </el-table-column>
       <!-- <el-table-column label="原始订单号" align="center" show-overflow-tooltip width="180">
          <template slot-scope="scope">
            <el-button type="text" @click="handleOrderView(scope.row)">{{ scope.row.basicNo }}</el-button>
          </template>
        </el-table-column> -->
       <el-table-column label="商城" width="125" align="center" prop="mallName" />
       <el-table-column label="供应商" width="185" show-overflow-tooltip  align="center" prop="shipperName" />
       <el-table-column label="业务日期" width="195"  align="center" prop="jobDate" />


    </el-table>

    </div>
  </div>
</template>

<script>
import SalesOrderApi from '@/api/order/SalesOrderApi'
import AccountingApApi from '@/api/accounting/AccountingApApi'
import SalesOrderItemApi from '@/api/order/SalesOrderItemApi'
import CustomerApi from '@/api/basic/CustomerApi'
import StoreApi from '@/api/basic/StoreApi'
import SupplierApi from '@/api/basic/SupplierApi'
import DictValueApi from '@/api/system/DictValueApi'
import appConstant from '@/constant/AppConstant'
import AuthUtils from '@/utils/AuthUtils'
export default {
  name: 'OmsSalesOrderList',
  filters: {
    settleStatusFilter(item) {
      const values = appConstant.settleStatusList.filter(value => value.key === item)
      if (values && values.length > 0) {
        return values[0].label
      } else {
        return ''
      }
    }
   },
  data() {
    return {
      options: {
        sysYnTypeOptions: [],
        orderTypeOptions: [],
        sourceTypeOptions: [],
        supplierIdOptions: [],
        settleStatusOptions: appConstant.settleStatusList,
        salesOrderStatusOptions: [],
        storeOptions: [],
        customers: [],
        payTypeOptions: []
      },
      queryParams: {
        customerNo: null,
        custSalesOrderNo: null,
        custSalesOrderItemNo: null,
        monthYear: null,
        custSpuId: null,
        custSkuId: null,
        storeId: null,
        sortProperty: null,
        sortDirection: null
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      uploadDialog: {
        title: '订单批量导入',
        visible: false
      },


      loading: true,
      exportLoading: false,
      appLoading: false,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true,
      amountSum: null,
      contractAmountSum: null,
      wtCntSum: null,
      returnAmountSum: null,
      sumSellRtAmt: null,
      returnCntSum: null,
      custMilePaymentAmt: null
    }
  },
  created() {
    this.initParam()
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    initParam() {
      this.queryParams.custSkuId =this.$route.params.skuId
      this.queryParams.monthYear = this.$route.params.monthYear
      console.log(this.queryParams)
    },
    loadOptions() {
      CustomerApi.list().then(response => {
        this.options.customers = response.data
      })
      StoreApi.list().then(response => {
        this.options.storeOptions = response.data
      })
    /*  SupplierApi.list().then(response => {
         this.options.supplierIdOptions = response.data
       }) */
      /* StoreApi.list().then(response => {
         that.options.storeOptions = response.data
       }) */
     /* DictValueApi.list({ dictCode: 'oms_sales_order_type' }).then(response => {
        this.options.orderTypeOptions = response.data
      })
      DictValueApi.list({ dictCode: 'oms_sales_order_source' }).then(response => {
        this.options.sourceTypeOptions = response.data
      })
      DictValueApi.list({ dictCode: 'oms_sales_order_status' }).then(response => {
        this.options.salesOrderStatusOptions = response.data
      })
      DictValueApi.list({ dictCode: 'sys_payment_type' }).then(response => {
        this.options.payTypeOptions = response.data
      }) */
    },

    handlePriceView(row) {
      this.$router.push({ path: `/materialBuyPrice/materialBuyPriceSpuList/${row.custSpuId}` })
    },




    handleQuery() {
      this.loading = true
      this.returnAmountSum=0.0
       this.returnCntSum =0.0
       this.sumSellRtAmt =0.0
       this.custMilePaymentAmt =0.0
       this.amountSum =0.0
       this.contractAmountSum = 0.0
       this.wtCntSum =0.0

     // this.queryParams.errorFlag = 'N'
      AccountingApApi.findSkuAll(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total

        this.loading = false

      }, () => {
        this.loading = false
      })
      AccountingApApi.settlementViewMonthlySum(this.queryParams).then(response => {
        this.amountSum = response.data.amountSum
        this.contractAmountSum = response.data.contractAmountSum
        this.wtCntSum = response.data.wtCntSum
        this.custMilePaymentAmt= response.data.custMilePaymentAmt
      })
      AccountingApApi.settlementViewMonthlyReturnSum(this.queryParams).then(response => {

          this.returnAmountSum= response.data.sumRtAmt
          this.sumSellRtAmt= response.data.sumSellRtAmt
         this.returnCntSum = response.data.sumRtQty


      })
    },

    handleSortChange(sort) {
      if (sort.order) {
        this.queryParams.sortProperty = sort.prop
        this.queryParams.sortDirection = sort.order === 'ascending' ? 'asc' : 'desc'
      } else {
        this.queryParams.sortProperty = null
        this.queryParams.sortDirection = null
      }
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },


  }
}
</script>
